// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import config from "../config/config";
import CookLevelUp from "./CookLevelUp";
import func from "../config/func";
import AudioManage from "../manage/AudioManage";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Cook extends cc.Component {

    n_info: cook;
    n_move: boolean = false;

    start() {
        this.node.on(cc.Node.EventType.TOUCH_END, this.levelUp, this);
    }

    init(date: cook) {
        this.n_info = date;
        this.node.children[0].active = false;
        this.n_move = false;
    }
    /**
     * 厨师升级
     */
    async levelUp() {
        if (config.openPopup) return;
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        config.openPopup = true;
        let spr: cc.SpriteFrame = this.node.getComponent(cc.Sprite).spriteFrame;
        let level = cc.find("Canvas/view/levelUp");
        if (level) {
            level.active = true;
            level.getComponent(CookLevelUp).init(this.n_info, false, spr);
        } else {
            let child: cc.Node = null;
            if (config.prefabArr[config.prefabPath.levelUp]) {
                child = cc.instantiate(config.prefabArr[config.prefabPath.levelUp]);
            } else {
                let prefab = await func.loadResouce(config.prefabPath.levelUp, cc.Prefab) as cc.Prefab;
                config.prefabArr[config.prefabPath.levelUp] = prefab;
                child = cc.instantiate(prefab);
            }
            let view = cc.find("Canvas/view");
            child.parent = view;
            child.getComponent(CookLevelUp).init(this.n_info, false, spr);
        }
    }
}
